package com.estronger.shopmanage.ui.install;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;
import com.estronger.shopmanage.bean.B33BuildNum;
import com.estronger.shopmanage.bean.BeanBase;
import com.estronger.shopmanage.fragment.FragmentFactory;
import com.estronger.shopmanage.fragment.install.BuildBaseFragment;
import com.estronger.shopmanage.manager.NetManager;
import com.estronger.shopmanage.manager.PopManager;
import com.estronger.shopmanage.ui.common.ConfirmActivity;
import com.estronger.shopmanage.utils.Toast;
import com.viewpagerindicator.TabPageIndicator;
import com.lidroid.xutils.http.RequestParams;

public class BuildActivity extends BaseActivity {

	private ImageView getState;
	private ViewPager main_VP;
	private ProgressBar progressbar;
	public static int CALL_RESULT = -1;
	// onActivityResult时 -SEND_CALL-代表电话结束后返回，-REFLESH_DATA-代表从其他界面返回要刷新界面
	public static int SEND_CALL = 10000, REFLESH_DATA = 5000;
	public static String send_deliv_id;// 提交给服务器的 deliv_id参数
	private PopManager popManager;// 弹窗manager
	private FrameLayout rl_root;// 根布局view

	private TabPageIndicator indicator;// 页面的title布局
	private TextView title;
	private int currPosition = 0;// 当前显示页面得位置
	// 对应每个页的位置-标题
	private String[] titleString = { "待领取(0)", "预约确认(0)", "安装确认(0)", "客户证言(0)", "全部" };
	private String[] titleString2 = {"待领取", "预约确认", "安装确认", "客户证言", "全部"};
	private String[] type = { "0", "1", "2", "5", "4" };

	// 初始化各个View
	@Override
	protected void initView() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		rl_root = (FrameLayout) View.inflate(this, R.layout.activity_send,
				null);
		setContentView(rl_root);

		progressbar = (ProgressBar) findViewById(R.id.progressBar1);
		main_VP = (ViewPager) findViewById(R.id.main_vp);
		indicator = (TabPageIndicator) findViewById(R.id.category_strip);
		getState = (ImageView) findViewById(R.id.copy_task_state);

		title = (TextView) findViewById(R.id.measure_title);
		title.setText("安装任务");

		// getTitleNum();
	}

	// 获取安装数量
	public void getTitleNum() {
		RequestParams params = new RequestParams();
		params.addBodyParameter("sf_action", "istgettasklistnum");
		params.addBodyParameter("usercode", MyApplication.getUserCode());
		connect(params, NetManager.BUILD_NUM);
	}

	// 主要是做子页面切换的监听事件
	@Override
	protected void initAction() {
		indicator.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(final int position) {
				currPosition = position;
				MyApplication.getHandler().postDelayed(new Runnable() {
					@Override
					public void run() {
						FragmentFactory.createBuildFragment(position).connect(
								type[position]);
					}
				}, 500);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		main_VP.setAdapter(new MeasureAdpater(getSupportFragmentManager()));
		indicator.setViewPager(main_VP);
		indicator.onPageSelected(0);
		indicator.setSmoothScrollingEnabled(true);
	}

	public int getCurrPosition() {
		return currPosition;
	}

	/**
	 * @param position
	 *            哪个页面 0-5
	 * @param size
	 *            页面里有多少条目
	 */
	public void setIndicatorTitle(int position, String size) {
		LinearLayout layout = ((LinearLayout) indicator.getChildAt(0));
		TextView tv = (TextView) (layout.getChildAt(position));
		tv.setText(titleString2[position] + "(" + size + ")");
	}

	// 安装下面五个fragment的适配器
	private class MeasureAdpater extends FragmentStatePagerAdapter {
		public MeasureAdpater(FragmentManager fm) {
			super(fm);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return titleString[position];
		}

		@Override
		public BuildBaseFragment getItem(int position) {
			BuildBaseFragment fragment = FragmentFactory
					.createBuildFragment(position);
			return fragment;
		}

		@Override
		public int getCount() {
			return titleString.length;
		}
	}

	// success==true 显示获取任务成功的图片 success==false 显示获取任务失败的图片
	public void getTaskResult(boolean success) {
		if (success) {
			getTitleNum();
			getState.setBackgroundResource(R.drawable.task_successful);
		} else {
			getState.setBackgroundResource(R.drawable.task_failure);
		}
		getState.setVisibility(View.VISIBLE);
		MyApplication.getHandler().postDelayed(new Runnable() {
			@Override
			public void run() {
				getState.setVisibility(View.GONE);
			}
		}, 1500);
	}

	// 根据visible设置进度小圆圈是否显示
	public void setProgresVisible(boolean visible) {
		if (visible) {
			progressbar.setVisibility(View.VISIBLE);
		} else {
			progressbar.setVisibility(View.INVISIBLE);
		}
	}

	// 联系客户按钮点击后调用
	public void contact_yes(View view) {
		if (CALL_RESULT != -1) {
			RequestParams params = new RequestParams();
			params.addBodyParameter("sf_action", "istlinkcst");
			params.addBodyParameter("usercode", MyApplication.getUserCode());
			params.addBodyParameter("dlvid", send_deliv_id);
			connect(params, NetManager.CONTACT);
			showDialog("提交数据中...", true);
		} else {
			Toast.show(this, "不支持的用户类型", 2000);
		}
	}

	/**
	 * 当前activity所有联网成功的回调
	 * 
	 * @params code 各个接口返回的唯一识别码，用来判断是哪个接口成功返回
	 * @params result 网络请求成功返回的数据
	 */
	@Override
	protected void onNetSuccess(int code, String result) {
		super.onNetSuccess(code, result);
		if (NetManager.CONTACT == code) {
			BeanBase bean = gson.fromJson(result, BeanBase.class);
			if (bean.success) {
				getState.setBackgroundResource(R.drawable.contact_successfu);
				FragmentFactory.createBuildFragment(getCurrPosition()).connect(
						type[getCurrPosition()]);
				getTitleNum();
			} else {
				getState.setBackgroundResource(R.drawable.contact_failure);
			}
			if (popManager == null) {
				popManager = new PopManager(this);
			}
			if (popManager.isContactPopShowing()) {
				popManager.hideContactPop();
			}
			getState.setVisibility(View.VISIBLE);
			MyApplication.getHandler().postDelayed(new Runnable() {
				@Override
				public void run() {
					getState.setVisibility(View.INVISIBLE);
				}
			}, 2000);
		}

		if (NetManager.BUILD_NUM == code) {
			B33BuildNum bean = gson.fromJson(result, B33BuildNum.class);
			if (bean.success) {
				setIndicatorTitle(0, bean.data.responsenum);
				setIndicatorTitle(1, bean.data.confirmnum);
				setIndicatorTitle(2, bean.data.finishnum);
				setIndicatorTitle(3, bean.data.testimony);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see FragmentActivity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int request, int result, Intent arg2) {
		super.onActivityResult(request, result, arg2);

		if (CALL_RESULT == request || SEND_CALL == request) {
			Intent intent = new Intent(this, ConfirmActivity.class);
			intent.putExtra("measureid", send_deliv_id);
			intent.putExtra("taskid", 3);
			startActivityForResult(intent, REFLESH_DATA);
		}
		if (REFLESH_DATA == result || REFLESH_DATA == request) {
			FragmentFactory.createBuildFragment(getCurrPosition()).connect(
					type[getCurrPosition()]);
			getTitleNum();
		}
	}

	@Override
	protected void onDestroy() {
		FragmentFactory.clearbuild();
		super.onDestroy();
	}
}
